package com.collect.cameroon.zhiScene.zhiWidget.zhiMenuView;

import android.content.Context;
import android.content.res.TypedArray;
import android.text.TextUtils;
import android.util.AttributeSet;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.collect.cameroon.R;

import butterknife.BindView;
import butterknife.ButterKnife;

public class ZHIMenuImageView extends LinearLayout implements View.OnClickListener {

    private final Context mContext;
    private View mView;
    private int icon;
    private int iconExt;
    private String mainText = "Title";
    private String detailText = "Detail";
    private boolean showSepLine = true;
    private boolean showIcon = true;
    private boolean showIconExt = true;
    private int mainTextColor = getResources().getColor(R.color.theme_black_0);
    private int detailTextColor = getResources().getColor(R.color.theme_black_1);
    private int sepLineColor = getResources().getColor(R.color.pay_line_color);

    @BindView(R.id.tv_main_title)
    TextView tv_main_title;
    @BindView(R.id.tv_detail_title)
    TextView tv_detail_title;
    @BindView(R.id.iv_icon)
    ImageView iv_icon;
    @BindView(R.id.iv_icon_ext)
    ImageView iv_icon_ext;

    private ZHIOnMenuItemDidChangedListener onMenuItemDidChangedListener;

    public ZHIMenuImageView(Context context) {
        this(context, null);
    }

    public ZHIMenuImageView(Context context, AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public ZHIMenuImageView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);

        this.mContext = context;

        TypedArray typedArray = context.obtainStyledAttributes(attrs, R.styleable.CFMenuImageView);

        icon = typedArray.getResourceId(R.styleable.CFMenuImageView_icon, R.mipmap.ic_launcher);
        iconExt = typedArray.getResourceId(R.styleable.CFMenuImageView_iconExt, R.drawable.ic_93);
        showIcon = typedArray.getBoolean(R.styleable.CFMenuImageView_showIcon, true);
        showIconExt = typedArray.getBoolean(R.styleable.CFMenuImageView_showExtIcon, true);

        mainText = typedArray.getString(R.styleable.CFMenuImageView_mainText);
        detailText = typedArray.getString(R.styleable.CFMenuImageView_detailText);

        mainTextColor = typedArray.getColor(R.styleable.CFMenuImageView_mainTextColor, getResources().getColor(R.color.theme_black_0));
        detailTextColor = typedArray.getColor(R.styleable.CFMenuImageView_detailTextColor, getResources().getColor(R.color.theme_black_1));

        sepLineColor = typedArray.getColor(R.styleable.CFMenuImageView_sepLineColor, getResources().getColor(R.color.pay_line_color));
        showSepLine = typedArray.getBoolean(R.styleable.CFMenuImageView_showSepLine, true);

        typedArray.recycle();

        initView();
    }

    @Override
    public void onClick(View v) {
        if(this.onMenuItemDidChangedListener != null){
            this.onMenuItemDidChangedListener.OnMenuItemDidChanged("");
        }
    }

    /**
     * 初始化组件数据
     * @type  method
     * @author liwenjie
     * @creatTime 2019-10-25 19:43
     */
    private void initView() {
        mView = View.inflate(mContext, R.layout.view_menu_image, this);
        ButterKnife.bind(this);

        iv_icon.setImageResource(icon);
        iv_icon.setVisibility(showIcon? VISIBLE:GONE);

        iv_icon_ext.setImageResource(iconExt);
        iv_icon_ext.setVisibility(showIconExt? VISIBLE:GONE);

        tv_main_title.setText(mainText);
        tv_main_title.setTextColor(mainTextColor);

        tv_detail_title.setText(detailText);
        tv_detail_title.setVisibility(VISIBLE);
        tv_detail_title.setTextColor(detailTextColor);
        if(TextUtils.isEmpty(detailText)){
            tv_detail_title.setVisibility(GONE);
        }

        mView.setOnClickListener(this);
    }

    /**
     * 设置显示内容
     * @type  method
     * @author liwenjie
     * @creatTime 2019-10-25 19:58
     */
    public void setText(String text){
        mainText = text;
        tv_main_title.setText(mainText);
    }

    /**
     * 获取显示内容
     * @type  method
     * @author liwenjie
     * @creatTime 2019-10-25 19:58
     */
    public String getText(){
        return tv_main_title.getText().toString().trim();
    }


    public void setCFOnMenuItemDidChangedListener(ZHIOnMenuItemDidChangedListener onMenuItemDidChangedListener){
        this.onMenuItemDidChangedListener = onMenuItemDidChangedListener;
    }

    public void setMainTextColor(int mainTextColor){
        this.mainTextColor = mainTextColor;
        tv_main_title.setTextColor(getResources().getColor(mainTextColor));
    }

    public void setDetailTextColor(int detailTextColor){
        this.detailTextColor = detailTextColor;
        tv_detail_title.setTextColor(getResources().getColor(detailTextColor));
    }
}


